package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.Mdzx;
import cn.stylefeng.guns.sys.modular.system.entity.ZxKemu;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxKemuMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxKemuParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxKemuResult;
import cn.stylefeng.guns.sys.modular.system.service.ZxKemuService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 装修科目表 服务实现类
 * </p>
 *
 * @author gyz
 * @since 2020-11-02
 */
@Service
public class ZxKemuServiceImpl extends ServiceImpl<ZxKemuMapper, ZxKemu> implements ZxKemuService {

    @Override
    public void add(ZxKemuParam zxKemuParam) {
        //完善pids,根据pid拿到pid的pids
        this.treeSetPids(zxKemuParam);

        //补充创建人信息
        zxKemuParam.setCreateUser(LoginContextHolder.getContext().getUserId());
        zxKemuParam.setCreateUserView(LoginContextHolder.getContext().getUser().getName());
        //科目级别
        String pids = zxKemuParam.getPids();
        int leveal = pids.split(",").length;
        zxKemuParam.setLevel(leveal+"");
        ZxKemu entity = getEntity(zxKemuParam);
        this.save(entity);
    }

    /**
     * 设置父级ids
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:58 PM
     */
    private void treeSetPids(ZxKemuParam zxKemuParam) {
        if (ToolUtil.isEmpty(zxKemuParam.getPid()) || zxKemuParam.getPid().equals(0L)) {
            zxKemuParam.setPid(0L);
            zxKemuParam.setPids("[0],");
        } else {
            Long pid = zxKemuParam.getPid();
            ZxKemu temp = this.getById(pid);
            String pids = temp.getPids();
            zxKemuParam.setPid(pid);
            zxKemuParam.setPids(pids + "[" + pid + "],");
        }
    }

    @Override
    public void delete(ZxKemuParam param) {
//        this.removeById(getKey(param));

        ZxKemu zxKemu = this.getById(getKey(param));
        //逻辑删除
        zxKemu.setDelFlag(1);
        zxKemu.setUpdateUser(LoginContextHolder.getContext().getUserId());
        zxKemu.setUpdateUserView(LoginContextHolder.getContext().getUser().getName());
        this.updateById(zxKemu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZxKemuParam param) {
        ZxKemu oldEntity = getOldEntity(param);
        //修改前级别
        String oldLevel = oldEntity.getLevel();
        //修改前父级
        Long oldPid = oldEntity.getPid();

        //完善pids,根据pid拿到pid的pids
        this.treeSetPids(param);
        int leveal = param.getPids().split(",").length;
        param.setLevel(leveal+"");
        ZxKemu newEntity = getEntity(param);
        //修改后级别
        String newLevel = newEntity.getLevel();
        //修改后父级
        Long newPid = newEntity.getPid();

        newEntity.setUpdateUser(LoginContextHolder.getContext().getUserId());
        newEntity.setUpdateUserView(LoginContextHolder.getContext().getUser().getName());
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);

        //二级科目父级变化之后，重新计算所有门店的投资情况的金额
        //TODO-1 修改前后都是二级，父级不同
//        if(oldLevel.equals(newLevel)&&oldLevel.equals("2")&&!oldPid.equals(newPid)){
//            //重新计算所有门店的投资情况金额
//            tzqkService.updateAllMdJineHeJi();
//        }


    }

    @Override
    public ZxKemuResult findBySpec(ZxKemuParam param) {
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<ZxKemuResult> findListBySpec(ZxKemuParam param) {
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ZxKemuParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ZxKemuParam param) {
        return param.getBtyZxKemuId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ZxKemu getOldEntity(ZxKemuParam param) {
        return this.getById(getKey(param));
    }

    private ZxKemu getEntity(ZxKemuParam param) {
        ZxKemu entity = new ZxKemu();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<LayuiTreeNode> layuiTree() {
        return this.baseMapper.layuiTree();
    }
    @Override
    public List<LayuiTreeNode> layuiTree(Long pinpai) {
        return this.baseMapper.layuiTreeByPinpai(pinpai);
    }



    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }
    @Override
    public List<ZTreeNode> tree(Long pinpai) {
        return this.baseMapper.treeByPinpai(pinpai);
    }
    @Override
    public ZxKemu getDetailById(Long btyZxKemuId) {
        ZxKemu detail = this.getById(btyZxKemuId);
        if (detail.getPid() != 0L && detail != null) {
            ZxKemu parentDetail = this.getById(detail.getPid());
            if (parentDetail != null) {
                detail.setPName(parentDetail.getName());
            }
        }

        return detail;
    }


}
